package com.gxa.eshops.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.gxa.eshops.admin.entity.domain.Goods;
import com.gxa.eshops.admin.mapper.GoodsMapper;
import com.gxa.eshops.admin.service.IGoodsService;
import com.gxa.eshps.cores.common.JsonResult;
import com.gxa.eshps.cores.exception.BusinessException;
import com.gxa.eshps.cores.exception.ExceptionCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Service;
import javax.jms.Queue;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author wangpeng
 * @since 2022-07-11
 */
@Service
@Slf4j
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {
    @Autowired
    @Qualifier("jmsMeTemplate")
    JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Queue messageQueue;

    @Override
    public Boolean updateAndDeleteCache(Goods goods) {
        //更新MySQL
        boolean saveOrUpdate = this.saveOrUpdate(goods);
        //删除缓存
        if (saveOrUpdate) {
            jmsMessagingTemplate.convertAndSend("goodsCache", goods.getGoodsId());
            return true;
        }
        return false;
    }

    @Override
    public Map<String, Object> addHotGoodsCache(Long... ids) throws Exception{
        JsonResult jsonResult = new JsonResult();
        int num = 0;
        for (Long id : ids) {
            Goods good = this.getById(id);
            if (good != null) {
                System.out.println("goods:"+good);
                ObjectMapper objectMapper = new ObjectMapper();
                /*//为jack json注册序列化提供能力的对象
                JavaTimeModule javaTimeModule = new JavaTimeModule();
                //系列化时间格式化
                javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-data:mm:ss")));
                //反序列化时间格式化
                javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-data:mm:ss")));
                //注册进入jackson
                objectMapper.registerModule(javaTimeModule);*/
                String goods = objectMapper.writeValueAsString(good);
                System.out.println("goods:"+goods);
                GenericMessage<String> string = new GenericMessage<>(goods);
                this.jmsMessagingTemplate.send(messageQueue, string);
            } else {
                num++;
                log.info("id为{}的商品添加热点商品失败", id);
            }
        }
        if (num > 0) {
            throw new BusinessException(ExceptionCode.OPERATION_FAILED);
        } else {
            jsonResult.setCode("1");
            jsonResult.setMsg("商品添加队列完成");
        }
        return jsonResult.getMap();
    }
}
